<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

/**
 * 商品类型模型
 * Class ZhunleGoodsType
 * @package App\Models\CacheModel
 */
class MallGoodsType extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.mall_goods_type';
    protected $cacheModel = 'goods.mall_goods_type';

    /**
     * 获取所有商品类型
     * @return array|mixed
     * @throws \Exception
     */
    public function getGoodsTypeList()
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setSort('DESC')->ZsGetIocData('list', function ()
        {
            $where = ['status' => 1];
            try
            {
                $ids = $this->where($where)->column('id', 'id');
                $ids = array_flip_key_value($ids);
            }
            catch (\Exception $e)
            {
                $ids = [];
            }
            return $ids;
        });
        $data_list['data'] = $this->batGetInfo($ids, ['name', 'description']);
        return $ids;
    }

    /**
     * 获取商品类型信息
     * @param $ids
     * @param $field
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function batGetInfo($ids, $field)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel)->batchGetCache($ids, function ($ids)
        {
            try
            {
                $data = $this->where(['id' => ['in', $ids]])->column('id,name,description', 'id');
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        }, $field);
    }
}
